
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  query.query;

import java.util.Collection;
import java.util.Hashtable;
import java.util.Set;
import java.util.Vector;

import property.Order;
import utility.CapitalChar;
import utility.ConvertToLower;

import database.RecordSearch;
import dbmanager.Column;
import dbmanager.GetIdName;



public class ParseQuery 
{
	
	Hashtable<Object, Object> QueryMap = new Hashtable<Object, Object>();
	
	GetIdName gid = new GetIdName();
	Order order ;
	String QUERY="query";
	String buffer[];
	String packagename ="query.query.";
	String object;
	String strCondition = "condition";
	String strFieldList="fieldlist";
	String Query="";
	String strJoin = "Join";
	String strTableName = "tablename";
	String StorageObject;
	
	public void SetCondition(Hashtable<Object, Object> HashCondition)
	{
			
		Vector<Object> VecCondition =new Vector<Object>();
		
		Hashtable<Object, Object> tempHash = new Hashtable<Object, Object>();
		
		
		Set<Object> setkey =  HashCondition.keySet();
		
		Object keys[] = setkey.toArray();
		
		if(QueryMap.containsKey(strCondition))		
			VecCondition.addAll((Collection<? extends Object>) QueryMap.get(strCondition));
			
		for(int i = 0;i < HashCondition.size(); i++)
		{
			tempHash = new Hashtable<Object, Object>();
			tempHash.put("conditionfield", keys[i].toString());
			tempHash.put("conditionvalue", HashCondition.get(keys[i].toString()));
			if(HashCondition.containsKey("conditionoperator")){
				tempHash.put("conditionoperator", HashCondition.get("conditionoperator"));
			}
			else{
				tempHash.put("conditionoperator","=");
			}
			VecCondition.add(tempHash);
		}
		this.QueryMap.put(strCondition,VecCondition);
	}
	
	public void SetFieldList(Hashtable<Object, Object> HashFieldList)
	{
		String id;
		Vector<Object> VecFieldList = new Vector<Object>();
		if(QueryMap.containsKey(strFieldList))
			VecFieldList = (Vector<Object>) QueryMap.get(strFieldList);
		id = gid.getId(HashFieldList.get("field").toString());
		HashFieldList.remove("field");
		HashFieldList.put("field", id);
		VecFieldList.add(HashFieldList);
		this.QueryMap.put(strFieldList,VecFieldList);
	}
	
	
	
/*
  
  
	public void setViewObject(String object, char ch) 
	{
	   this.StorageObject = gid.getId(object);	
	}

	
*/	
	public void SetQueryMap(Hashtable<Object, Object> HashJoin)
	{
		this.QueryMap.putAll(HashJoin);
	}
	
	public void setResultSet(String result)
	{
		Vector<Object> vecResult = new Vector<Object>();
		vecResult.add(result);
		this.QueryMap.put("Resultset", vecResult);
	}
	
	
	public void SetJoin(Hashtable<Object, Object> HashJoin)
	{
		Vector<Object> VecJoin = new Vector<Object>();
		VecJoin.add(HashJoin);
		this.QueryMap.put(strJoin, VecJoin);
	}
	
	public void SetTableName(String tablename)
	{	
		Vector<Object> VecTableName = new Vector<Object>();
		VecTableName.add(tablename);
		this.QueryMap.put(strTableName, VecTableName);
	}
	
	public void processQuery()
	{
	/*	Queryinterface query;
		
		String tablename = "";
		String typeofreport = ""; 
		Hashtable<Object, Object> table;
		order =new Order();
		QUERY = gid.getId(QUERY);
		table = order.getSequences(QUERY);
		
		for(int i=0;i<table.size();i++)
		{
			try 
			{
				String classname = table.get(Integer.toString(i+1)).toString().toLowerCase();
				System.out.println("Class Name is "+classname);
				if(QueryMap.containsKey(classname))
				{	
					Class c =Class.forName(packagename+CapitalChar.makeFirstCharCapital(classname));
					query = (Queryinterface) c.newInstance();
					query.initPool();
					query.setViewObject((Vector<Object>)QueryMap.get(classname));
					query.setbuffer(buffer);
					query.settablename(tablename);
					query.setQuery(Query);
					query.initializeData();	
					query.settypeofreport(typeofreport);
					query.updateReport();
					buffer = query.getbuffer();
					tablename = query.gettablename();
					Query = query.getQuery();
				}
			}
			catch (Exception e) 
			{
				e.printStackTrace();
			}
		}
		*/
		QueryMap = gid.ConvertKeytoMasterId(QueryMap);
		query.query.Query query = new query.query.Query();
		query.initPool();
		query.setQueryMap(QueryMap);
		query.initializeData();		
		query.updateReport();
		this.buffer= query.getbuffer();
		
		for(int i=0;i<buffer.length;i++)
		{
			System.out.println(buffer[i]);
		}		
	}
	
	public String [] getbuffer()
	{
		return buffer;
	}
	
	public static void main(String argv[] )
	{
		/*	
		Hashtable<Object, Object> HashTest = new Hashtable<Object, Object>();
		HashTest.put("pid", "9");
		HashTest.put("pv", "25");
		//HashTest.put("type", "item");
		ob.SetCondition(HashTest);
		HashTest = new Hashtable<Object, Object>();
		HashTest.put("field", "mastername");		
		ob.SetFieldList(HashTest);
		HashTest =new Hashtable<Object, Object>();
		HashTest.put("field", "itemcode");
		ob.SetFieldList(HashTest);
		ob.SetTableName("master_table");
		/*
		
		
		ob.setViewObject("xyz", 'a');
		Vector<Object> vector = new Vector<Object>();
		vector.add("itemreqreport");
		HashTest.put("resultset",vector);
		Vector<Object> vector1 = new Vector<Object>();
		vector1.add(HashTest);
		Hashtable<Object, Object> HashTestquery = new Hashtable<Object, Object>();
		ob.QueryMap.put("query",vector1);
		
		
		ob.processQuery();
		ParseQuery pq =new ParseQuery();		
		Hashtable<Object, Object>  table1= new Hashtable<Object, Object>();
		Vector<Object> result = new Vector<Object>();
		result.add("employeeinformation");
		table1.put("resultset", result);
		Vector<Object> join = new Vector<Object>();
		Hashtable<Object , Object> tab=new Hashtable<Object, Object>();
		tab.put("lhs", "userid");
		tab.put("rhs", "personcode");
		join.add(tab);
		table1.put("join", join);
		Vector<Object> Query = new Vector<Object>();		
		Hashtable<Object, Object> Query1 = new Hashtable<Object, Object>();
		Vector<Object> result1 = new Vector<Object>();
		result1.add("poreport");
		
		Vector<Object>  query2 = new Vector<Object>();
		Vector<Object> join1 = new Vector<Object>();
		Hashtable<Object , Object> tab1=new Hashtable<Object, Object>();
		tab1.put("lhs", "userid");
		tab1.put("rhs", "personcode");
		join1.add(tab1);
		Query1.put("resultset",result1);
		Query1.put("join",join1);
		Hashtable<Object, Object> querymap2 = new Hashtable<Object, Object>();
		Vector<Object> result2 = new Vector<Object>();
		result2.add("potdreport");
		
		
		querymap2.put("resultset", result2);
		
		query2.add(querymap2);
		Query1.put("query", query2);
		Query.add(Query1);
		table1.put("query",Query);
		pq.SetQueryMap(table1);
		pq.processQuery();*/
		
		ParseQuery pq =new ParseQuery();		
		Hashtable<Object, Object>  table1= new Hashtable<Object, Object>();
		Vector<Object> result = new Vector<Object>();
		//result.add("employeeinformation");
		///table1.put("resultset", result);
		Hashtable<Object , Object> iinnerresultsethash=new Hashtable<Object, Object>();
		Vector<Object> iinnerresultvector = new Vector<Object>();
		Vector<Object> iinnerqueryvector = new Vector<Object>();
		Vector<Object> iinnerresultvector1 = new Vector<Object>();
		iinnerresultvector1.add("potdreport");
		iinnerresultsethash.put("resultset", iinnerresultvector1);
		iinnerqueryvector.add(iinnerresultsethash);
		Vector<Object> join = new Vector<Object>();
		Hashtable<Object , Object> tab=new Hashtable<Object, Object>();
		Hashtable<Object , Object> innerresultsethash=new Hashtable<Object, Object>();
		Vector<Object> innerresultvector = new Vector<Object>();
		Vector<Object> innerresultvector1 = new Vector<Object>();
		innerresultvector1.add("employeeinformation");
		innerresultsethash.put("resultset", innerresultvector1);
		Hashtable<Object, Object> innerjoin = new Hashtable<Object, Object>();
		Vector<Object> innerjoinvector= new Vector<Object>();
		innerjoin.put("lhs", "userid");
		innerjoin.put("rhs", "personcode");
		innerjoinvector.add(innerjoin);
		innerresultsethash.put("join",innerjoinvector);
		innerresultsethash.put("query",iinnerqueryvector);
		innerresultvector.add(innerresultsethash);
		Hashtable<Object , Object> innerqueryhash=new Hashtable<Object, Object>();
		Vector<Object> innerqueryvector = new Vector<Object>();
		innerqueryhash.put("query",innerresultvector);
		innerqueryvector.add(innerqueryhash);
		Hashtable<Object , Object> outeresultsethash=new Hashtable<Object, Object>();
		Vector<Object> outerresultvector = new Vector<Object>();
		Hashtable<Object , Object> joinhash=new Hashtable<Object, Object>();
		Vector<Object> joinvector = new Vector<Object>();
		outerresultvector.add("poreport");
		outeresultsethash.put("resultset",outerresultvector);
		
		joinhash.put("lhs", "userid");
		joinhash.put("rhs", "personcode");
		joinvector.add(joinhash);		
		Hashtable<Object , Object> queryhash=new Hashtable<Object, Object>();
		queryhash.put("query", innerqueryvector);
		queryhash.put("resultset", outerresultvector);
		queryhash.put("join", joinvector);
		pq.SetQueryMap(queryhash);
		pq.processQuery();
		
		
	}
}
























